import APIClient from '../../network/index';

const Class = {
    state: {
        category: [],
        firstLevel: [],
        secondLevel: []
    },
    mutations: {
        classList(state, items) {
            state.firstLevel=[]
            state.category = [...items];
            state.firstLevel.push({
                name: "所有分类"
            })
            items.forEach((element) => {
                if (element.pid === 0) {
                    state.firstLevel.push(element)
                } else {
                    state.secondLevel.push(element)
                }
            })
        },
        CHANGE_SELECH_CATEGORY(state, item) {
            state.secondLevel = [];
            if (item.name === "所有分类") {
                state.category.forEach((element) => {
                    if (element.pid != 0) {
                        state.secondLevel.push(element)
                    }
                })
            } else {
                state.category.forEach((element) => {
                    if (element.pid === item.id) {
                        state.secondLevel.push(element)
                    }
                })
            }
        }
    },
    actions: {
        // 分类
        classList(context) {
            APIClient.classList().then((res) => {
                // console.log(res)
                context.commit("classList", res.data.data)
            })
        },
        CHANGE_SELECH_CATEGORY(context, item) {
            context.commit("CHANGE_SELECH_CATEGORY", item)
        }
    },
    modules: {

    }

}
export default Class